using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
namespace ChuanPlus.FormControl
{
    public partial class ControlReceiptVoucher : DevExpress.XtraEditors.XtraUserControl
    {
        DataTable m_dtUpaidOrder = null;
        MySql.Data.MySqlClient.MySqlParameter[] parameters = null;
        Users currentUser = null;
        public ControlReceiptVoucher(Users user)
        {
            parameters = new MySql.Data.MySqlClient.MySqlParameter[1];
            parameters[0] = new MySql.Data.MySqlClient.MySqlParameter("?customerID", MySql.Data.MySqlClient.MySqlDbType.Int32);

            InitializeComponent();
            comboCustomer.Properties.Items.AddRange(xpColCustomer);
            comboCustomer.SelectedIndex = 0;
            comboReceivedBy.Properties.Items.AddRange(xpColUsers);
            dateFrom.DateTime = dateTo.DateTime = DateTime.Today;
            LoadReceipt();
            this.currentUser = user;
        }

        private void ButtonSubmit_Click(object sender, EventArgs e)
        {
            if (validationForm())
            {
                DialogResult res = MessageBox.Show("Are you sure want to create new receipt for customer " + comboCustomer.Text,"Message",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
                if(res == DialogResult.OK)
                {
                    ReceiptVoucher rv = new ReceiptVoucher((Customer)comboCustomer.SelectedItem, dateReceipt.DateTime, Convert.ToDouble(textAmount.Text), (Users)comboReceivedBy.SelectedItem, currentUser);
                    rv.Save();
                    xpColReceiptVoucher.Reload();
                    grid.RefreshDataSource();
                }
            }
        }

        private void comboCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {        
            Customer c = comboCustomer.SelectedItem as Customer;
            if (c != null)
            {
                parameters[0].Value = c.CustomerID;
                m_dtUpaidOrder = DataProvider.execute("spGetUnpaidOrder", parameters);
                gridTop.DataSource = m_dtUpaidOrder;
                if (m_dtUpaidOrder.Rows.Count == 0)
                    ButtonSubmit.Enabled = false;
                else
                    ButtonSubmit.Enabled = true;
            }
        }
        private void LoadReceipt()
        {
            xpColReceiptVoucher.Filter = new BetweenOperator("ReceiptDate", dateFrom.DateTime, dateTo.DateTime);
        }

        private void buttonGo_Click(object sender, EventArgs e)
        {
            xpColReceiptVoucher.Filter = new BetweenOperator("ReceiptDate", dateFrom.DateTime, dateTo.DateTime);
        }

        private void textAmount_EditValueChanged(object sender, EventArgs e)
        {
            if(textAmount.Text != "")
                textAmount.Text = String.Format("{0:n2}", Double.Parse(textAmount.Text));
        }

        #region validation
           

        private void comboCustomer_Validating(object sender, CancelEventArgs e)
        {
            Customer c = comboCustomer.SelectedItem as Customer;
            if (c == null)
            {
                e.Cancel = true;
                comboCustomer.ErrorText = "Customer " + comboCustomer.Text + " does not exsit";
            }
        }

        private void comboReceivedBy_Validating(object sender, CancelEventArgs e)
        {
            Users u = comboReceivedBy.SelectedItem as Users;
            if (u == null)
            {
                e.Cancel = true;
                comboReceivedBy.ErrorText = "User " + comboReceivedBy.Text + " does not exsit";
            }
        }
        private bool validationForm()
        {
            string errorObj = "";
            bool valid = true;
            if (comboCustomer.Text == "")
            {
                errorObj = " customer "; valid = false;
            }
            else if (comboReceivedBy.Text == "")
            {
                errorObj = " Received by "; valid = false;
            }
            else if (textAmount.Text == "")
            {
                errorObj = " Amount "; valid = false;
            }
            else if (dateReceipt.Text == "")
            {
                errorObj = " Receipt date "; valid = false;
            }
            if (!valid)
                MessageBox.Show("Please enter" + errorObj, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return valid;
        }
        #endregion

        private void ButtonReset_Click(object sender, EventArgs e)
        {
            reset();
        }
        private void reset()
        {
            textAmount.Text = "";
            dateReceipt.Text = "";
            comboReceivedBy.Text = "";
        }
    }
}
